﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SZSLSystem.Common;

namespace SZSLSystem.Web.Admin.DataDictionary
{
    public partial class Add : SZSLSystem.Web.UI.ManagePage
    {
        #region 全局变量

        /// <summary>
        /// 商品类别业务逻辑
        /// </summary>
        private readonly SZSLSystem.BLL.DataDictionary bll = new SZSLSystem.BLL.DataDictionary();
        /// <summary>
        /// 父ID
        /// </summary>
        public string pId;
        /// <summary>
        /// 顶级类别
        /// </summary>
        public string pName = "数据字典";

        #endregion 

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.Params["ParentID"]))
                {
                    pName = bll.GetModel(Guid.Parse(Request.Params["ParentID"])).TypeName;
                    pId = Request.Params["ParentID"].ToString();
                }
                lblPid.Text = pId;
                lblPname.Text = pName;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SZSLSystem.Model.DataDictionary model = new SZSLSystem.Model.DataDictionary();
                string parentId = this.lblPid.Text.Trim();          //父ID

                model.ID = Guid.NewGuid();
                model.TypeName = this.txtName.Text.Trim();
                if (!string.IsNullOrEmpty(parentId))
                {
                    SZSLSystem.Model.DataDictionary pModel = bll.GetModel(Guid.Parse(parentId));

                    if (pModel != null)
                    {
                        model.ClassLayer = pModel.ClassLayer + 1;
                    }
                    model.ParentID = Guid.Parse(parentId);
                }
                else
                {
                    model.ClassLayer = 1;
                    model.ParentID = Guid.Parse(DataDictionaryID);//顶级类别编号
                }
                model.Description = this.txtDescription.Text.Trim();
                model.DataCode = this.txtDataCode.Text.Trim();
                model.Sort = int.Parse(this.txtSortId.Text.Trim());
                bll.Add(model);
                //
                //日志写入
                //
                SaveLogs("添加数据字典成功！", 0);

                JscriptPrint("添加数据字典成功啦！", "List.aspx", "Success");
            }
            catch (Exception err)
            {
                //
                //异常日志写入
                //
                SaveLogs(err.ToString(), 1);

                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>删除数据字典失败啦！", "", "Error");
            }
        }
    }
}
